import axios from "axios"
import router from "@/router"
import { useCookieStore } from "@/stores/cookieStore"
import { ElMessage } from 'element-plus'

const request = axios.create({
  baseURL: 'https://service-rur9ving-1304986681.gz.apigw.tencentcs.com'
  // https://service-3rkpsk0a-1304986681.gz.apigw.tencentcs.com/release/
  // http://localhost:3000
  // http://192.168.0.16:5173/
})

request.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
request.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  const useCookie = useCookieStore()
  console.log(error);
  if (error.code === 'ERR_NETWORK') {
    ElMessage({
      message: '网络连接失败！',
      grouping: true
    })
  }
  if (error.response.data.code === 301) {
    useCookie.clearCookie()
    ElMessage('登录已过期，请重新登录。')
    router.replace('/login')
  }
  return Promise.reject(error);
});

export default request